package cn.tsinghua.comsmacitccntsinghua.tinker;

/**
 * @Author yjl
 * @create 2021/10/12 19:48
 */
import android.util.Log;

import com.tencent.tinker.lib.service.DefaultTinkerResultService;
import com.tencent.tinker.lib.service.PatchResult;
import com.tencent.tinker.lib.util.TinkerLog;
import com.tencent.tinker.lib.util.TinkerServiceInternals;

import java.io.File;

/**
 * optional, you can just use DefaultTinkerResultService
 * we can restart process when we are at background or screen off
 * Created by zhangshaowen on 16/4/13.
 */
public class SampleResultService extends DefaultTinkerResultService {
    private static final String TAG = "Tinker.SampleResultService";
    private static boolean isUploaded = false;

    @Override
    public void onPatchResult(final PatchResult result) {
        if (result == null) {
            TinkerLog.e(TAG, "SampleResultService received null result!!!!");
            return;
        }
        TinkerLog.i(TAG, "SampleResultService receive result: %s", result.toString());

        //first, we want to kill the recover process
        TinkerServiceInternals.killTinkerPatchServiceProcess(getApplicationContext());

        //在这里自定义处理加载成功和失败
        String log = result.isSuccess ? "patch success, please restart process  补丁加载成功，请重启应用" : "patch fail, please check reason  补丁加载失败!!!!!!!!!!!";


        Log.d("succ",log);
        if(!isUploaded){
            isUploaded = true;
        }

//        try {
//            if (!isUploaded) {
//                StringExtendFunKt.showDebugToast(log);
//                StringExtendFunKt.w(log, "lllttt_tinker");
//                UmengDiyEventUtilKt.actionEvent(SampleApplicationLike.getApp(),
//                        "updateHotFix",
//                        new UmengEventBean("fixSuccess", result.isSuccess),
//                        new UmengEventBean("systemInfo", JSONObject.toJSONString(YxUtil.getInstance().getInputDevice()))
//                );
//            }
//            isUploaded = true;
//        } catch (Exception e) {
//            AnyExtendFunKt.upload(e);
//        }

        // is success and newPatch, it is nice to delete the raw file, and restart at once
        // for old patch, you can't delete the patch file
        if (result.isSuccess) {
            deleteRawPatchFile(new File(result.rawPatchFilePath));

            //not like TinkerResultService, I want to restart just when I am at background!
            //if you have not install tinker this moment, you can use TinkerApplicationHelper api
            if (checkIfNeedKill(result)) {
                if (Utils.isBackground()) {
                    TinkerLog.i(TAG, "it is in background, just restart process");
                    restartProcess();
                } else {
                    //we can wait process at background, such as onAppBackground
                    //or we can restart when the screen off
                    TinkerLog.i(TAG, "tinker wait screen to restart process");
                    new Utils.ScreenState(getApplicationContext(), new Utils.ScreenState.IOnScreenOff() {
                        @Override
                        public void onScreenOff() {
                            restartProcess();
                        }
                    });
                }
            } else {
                TinkerLog.i(TAG, "I have already install the newly patch version!");
            }
        }
    }

    /**
     * you can restart your process through service or broadcast
     */
    private void restartProcess() {
        TinkerLog.i(TAG, "app is background now, i can kill quietly");
        //you can send service or broadcast intent to restart your process
        android.os.Process.killProcess(android.os.Process.myPid());
    }

}